//
//  ListViewController.m
//  DolphinMusic
//
//  Created by dllo on 15/3/23.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "ListViewController.h"
#import "ListDetailsTableViewCell.h"
#import "Song.h"
#import "MusicPlayViewController.h"


@interface ListViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain)UIScrollView *scrollView;
@property (nonatomic, retain)UIImageView *bgImageView;
@property (nonatomic, retain)UIButton *backButton;
@property (nonatomic, retain)UILabel *titleLabel;
@property (nonatomic, retain)UILabel *pubDateLabel;
@property (nonatomic, retain)UIButton *playButton;
@property (nonatomic, retain)UILabel *numberLabel;
@property (nonatomic, retain)UITableView *songTableView;
@property (nonatomic, retain)UILabel *changeLabel1;
@property (nonatomic, retain)UIView *view2;

@property (nonatomic, assign)BOOL isLabelExtend;


@property (nonatomic, retain)NSString *imageStr;
@property (nonatomic, retain)NSString *title1;
@property (nonatomic, retain)NSString *pubDate;
@property (nonatomic, retain)NSString *numberStr;
@property (nonatomic, retain)NSString *descStr;



@property (nonatomic, retain)NSMutableArray *songArr;


@property (nonatomic, retain)MBProgressHUD *HUD1;



@end


CGFloat bb1 = 70.0 / 667;


@implementation ListViewController

-(void)dealloc
{
    [_HUD1 release];
    [_songArr release];
    [_descStr release];
    [_numberStr release];
    [_pubDate release];
    [_title1 release];
    [_imageStr release];
    [_view2 release];
    [_changeLabel1 release];
    [_scrollView release];
    [_bgImageView release];
    [_backButton release];
    [_titleLabel release];
    [_pubDateLabel release];
    [_playButton release];
    [_numberLabel release];
    [_songTableView release];
    [_addressStr release];
    [super dealloc];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UIWindow *window = [[[UIApplication sharedApplication] windows] firstObject];
    PlayView *playView = (PlayView*)[window viewWithTag:10001];
    playView.target = self.navigationController;
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 20, VIEW_WIDTH, VIEW_HEIGHT - 20 - bb1 * VIEW_HEIGHT)];
    self.scrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.scrollView];
    [_scrollView release];
    
    
    self.HUD1 = [[MBProgressHUD alloc] initWithView:self.scrollView];
    [self.scrollView addSubview:self.HUD1];
    self.HUD1.labelText = @"Music is on the way";
    self.HUD1.dimBackground = YES;
    [self.HUD1 show:YES];
    [self doTask];
    
}

- (void)doTask
{
    self.backButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.backButton.frame = CGRectMake(10, 30, 50, 44);
    
//    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeSystem];
//    leftButton.frame = CGRectMake(0, 20, 50, 44);
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 12, 20, 20)];
    imageView.image = [UIImage imageNamed:@"backButton.png"];
    [self.backButton addSubview:imageView];
    [imageView release];
    
//    [self.backButton setBackgroundImage:[UIImage imageNamed:@"backButton.png"] forState:UIControlStateNormal];
    [self.backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.backButton];
    
    self.songArr = [NSMutableArray array];
    [NetHandler getDataWithUrl:self.addressStr completion:^(id dict) {
        
        
        if ([dict objectForKey:@"refresh"]) {
            
            self.imageStr = [dict objectForKey:@"pic_s640"];
            self.title1 = [dict objectForKey:@"name"];
            
            NSString *str1 = [dict objectForKey:@"createTime"];
            self.pubDate = [NSString stringWithFormat:@"发布时间: %@", str1];
            
            NSDictionary *dic = [[dict objectForKey:@"list"] firstObject];
            NSArray *arr = [dic objectForKey:@"songList"];
            
            self.numberStr = [NSString stringWithFormat:@"共%ld首歌", arr.count];
            
            self.descStr = [dict objectForKey:@"desc"];

            for (NSDictionary *dic11 in arr) {
                Song *song = [[Song alloc] initWithDictionary:dic11];
                [self.songArr addObject:song];
                [song release];
            }
        }
        if ([dict objectForKey:@"listid"]) {
            self.imageStr = [dict objectForKey:@"pic_500"];
            self.title1 = [dict objectForKey:@"title"];
            self.pubDate = [dict objectForKey:@"tag"];
            NSArray *arr = [dict objectForKey:@"content"] ;
            NSInteger a = [arr count];
            self.numberStr = [NSString stringWithFormat:@"共%ld首歌", a];
            self.descStr = [dict objectForKey:@"desc"];
            for (NSDictionary *dic11 in arr) {
                Song *song = [[Song alloc] initWithDictionary:dic11];
                [self.songArr addObject:song];
                [song release];
            }
        }
        if ([dict objectForKey:@"albumInfo"]) {
            NSDictionary *dic1 = [dict objectForKey:@"albumInfo"];
            self.imageStr = [dic1 objectForKey:@"pic_s500"];
            self.title1 = [dic1 objectForKey:@"title"];
            NSString *str1 = [dic1 objectForKey:@"publishtime"];
            self.pubDate = [NSString stringWithFormat:@"发行时间: %@", str1];
            NSArray *arr = [dict objectForKey:@"songlist"];
            NSInteger a = [arr count];
            self.numberStr = [NSString stringWithFormat:@"共%ld首歌", a];
            NSString *str = [dic1 objectForKey:@"info"];
            if (str == nil || [str isEqualToString:@""]) {
                self.descStr = @"简介: 暂无";
            }
            else {
                self.descStr = [NSString stringWithFormat:@"简介: %@", str];
            }
            for (NSDictionary *dic11 in arr) {
                Song *song = [[Song alloc] initWithDictionary:dic11];
                [self.songArr addObject:song];
                [song release];
            }
        }
        
        
        self.bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -20, VIEW_WIDTH, 220)];
        self.bgImageView.backgroundColor = [UIColor whiteColor];
        [self.bgImageView setImageWithURL:[NSURL URLWithString:[self.imageStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
        [self.scrollView addSubview:self.bgImageView];
        [_bgImageView release];
        
        self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 165, VIEW_WIDTH - 60, 20)];
        self.titleLabel.text = self.title1;
        self.titleLabel.textColor = [UIColor whiteColor];
        self.titleLabel.font = [UIFont fontWithName:@"Marion-BOLD" size:20];
        [self.scrollView addSubview:self.titleLabel];
        [_titleLabel release];
        
        self.pubDateLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 183, VIEW_WIDTH - 60, 15)];
        self.pubDateLabel.textColor = [UIColor whiteColor];
        self.pubDateLabel.text = self.pubDate;
        self.pubDateLabel.font = [UIFont systemFontOfSize:14];
        [self.scrollView addSubview:self.pubDateLabel];
        [_pubDateLabel release];
        
        self.playButton = [UIButton buttonWithType:UIButtonTypeSystem];
        self.playButton.frame = CGRectMake(VIEW_WIDTH - 50, 160, 35, 35);
        [self.playButton setBackgroundImage:[UIImage imageNamed:@"playAll.png"] forState:UIControlStateNormal];
        [self.scrollView addSubview:self.playButton];
        [self.playButton addTarget:self action:@selector(playAllButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        
        
        UIView *coverView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 200, VIEW_WIDTH, 45)];
        coverView1.backgroundColor = [UIColor whiteColor];
        [self.scrollView addSubview:coverView1];
        [coverView1 release];
        
        self.numberLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 15, 100, 20)];
        self.numberLabel.font = [UIFont systemFontOfSize:15];
        self.numberLabel.text = self.numberStr;
        [coverView1 addSubview:self.numberLabel];
        [_numberLabel release];
        
        UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 44, VIEW_WIDTH, 1)];
        view1.backgroundColor = [UIColor blackColor];
        view1.alpha = 0.1;
        [coverView1 addSubview:view1];
        [view1 release];
        
        
        self.changeLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(15, 245, VIEW_WIDTH - 30, 45)];
        self.changeLabel1.text = self.descStr;
        self.changeLabel1.backgroundColor = [UIColor whiteColor];
        self.changeLabel1.userInteractionEnabled = YES;
        self.changeLabel1.font = [UIFont systemFontOfSize:15];
        self.changeLabel1.numberOfLines = 2;
        [self.scrollView addSubview:self.changeLabel1];
        [_changeLabel1 release];

        UIView *coverView2 = [[UIView alloc] initWithFrame:CGRectMake(0, 245, 15, 45)];
        coverView2.backgroundColor = [UIColor whiteColor];
        [self.scrollView addSubview:coverView2];
        [coverView2 release];
        
        CGRect rect = [self.changeLabel1.text boundingRectWithSize:CGSizeMake(VIEW_WIDTH - 30, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil];
        if (rect.size.height > 40) {
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changeLabeltapAction:)];
            [self.changeLabel1 addGestureRecognizer:tap];
            [tap release];
        }
        
        
        self.view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 289, VIEW_WIDTH, 1)];
        self.view2.backgroundColor = [UIColor blackColor];
        self.view2.alpha = 0.1;
        [self.scrollView addSubview:self.view2];
        [_view2 release];
        
        
        self.songTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 290, VIEW_WIDTH - 10, self.songArr.count * 60) style:UITableViewStylePlain];
        self.songTableView.delegate = self;
        self.songTableView.dataSource = self;
        self.songTableView.rowHeight = 60;
        
        [self.songTableView registerClass:[ListDetailsTableViewCell class] forCellReuseIdentifier:@"listDetailsReuse"];
        [self.scrollView addSubview:self.songTableView];
        [_songTableView release];
        
        self.scrollView.contentSize = CGSizeMake(0, self.songTableView.frame.origin.y + self.songTableView.frame.size.height - 50);
        
        self.songTableView.scrollEnabled = NO;
        
        
        if (self.songTableView.frame.size.height < self.scrollView.frame.size.height - 290) {
            UIView *view112 = [[UIView alloc] initWithFrame:CGRectMake(0, self.songTableView.frame.size.height + 290, VIEW_WIDTH, self.scrollView.frame.size.height - 290 - self.songTableView.frame.size.height)];
            view112.tag = 112;
            view112.backgroundColor = [UIColor whiteColor];
            [self.scrollView addSubview:view112];
            [view112 release];
        }
        
        [self.HUD1 hide:YES];
    }];
}

- (void)playAllButtonAction:(UIButton *)button
{
    MusicPlayViewController *musicPlayVC = [MusicPlayViewController shareInstance];
    Song *song = [self.songArr firstObject];
    musicPlayVC.playingNumber = 0;
    
    musicPlayVC.playSongArr = [NSMutableArray array];
    
    for (Song *song in self.songArr) {
        NSString *songID = song.song_id;
        [musicPlayVC.playSongArr addObject:songID];
    }
    musicPlayVC.address = [NSString stringWithFormat:@"http://tingapi.ting.baidu.com/v1/restserver/ting?from=webapp_music&method=baidu.ting.song.play&format=json&callback=&songid=%@&_=1413017198449", song.song_id];
    musicPlayVC.songTitle = song.title;
    musicPlayVC.type = @"music";
    musicPlayVC.playType = NO;
    musicPlayVC.songID = song.song_id;

}


// 点击更改frame
- (void)changeLabeltapAction:(UITapGestureRecognizer *)tap
{
    UIView *view113 = [self.view viewWithTag:112];
    [view113 removeFromSuperview];

    
    self.isLabelExtend = !self.isLabelExtend;
    if (self.isLabelExtend) {
    CGRect rect = [self.changeLabel1.text boundingRectWithSize:CGSizeMake(VIEW_WIDTH - 30, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil];
        self.changeLabel1.frame = CGRectMake(15, 245, VIEW_WIDTH- 30, rect.size.height);
        self.changeLabel1.numberOfLines = 0;
        
        self.view2.frame = CGRectMake(0, 245 + rect.size.height - 1, VIEW_WIDTH, 1);
        
        self.songTableView.frame = CGRectMake(0, self.view2.frame.origin.y + 1, VIEW_WIDTH - 10, self.songArr.count * 60);
        
        self.scrollView.contentSize = CGSizeMake(0, self.songTableView.frame.origin.y + self.songTableView.frame.size.height - 50);

    }
    else {
        self.changeLabel1.frame = CGRectMake(15, 250, VIEW_WIDTH - 30, 40);
        self.changeLabel1.numberOfLines = 2;
        self.view2.frame = CGRectMake(0, 289, VIEW_WIDTH, 1);
        self.songTableView.frame = CGRectMake(0, 290, VIEW_WIDTH - 10, 60 * self.songArr.count);
        
        self.scrollView.contentSize = CGSizeMake(0, self.songTableView.frame.origin.y + self.songTableView.frame.size.height - 50);
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.songArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ListDetailsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"listDetailsReuse" forIndexPath:indexPath];
    cell.song = [self.songArr objectAtIndex:indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    MusicPlayViewController *musicPlayVC = [MusicPlayViewController shareInstance];
    Song *song = [self.songArr objectAtIndex:indexPath.row];
    musicPlayVC.playingNumber = indexPath.row;
    
    musicPlayVC.playSongArr = [NSMutableArray array];
    
    for (Song *song in self.songArr) {
        NSString *songID = song.song_id;
        [musicPlayVC.playSongArr addObject:songID];
    }
    musicPlayVC.address = [NSString stringWithFormat:@"http://tingapi.ting.baidu.com/v1/restserver/ting?from=webapp_music&method=baidu.ting.song.play&format=json&callback=&songid=%@&_=1413017198449", song.song_id];
    musicPlayVC.songTitle = song.title;
    musicPlayVC.type = @"music";
    musicPlayVC.songID = song.song_id;

}

- (void)backButtonAction:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewWillAppear:(BOOL)animated
{
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    UIButton *button = (UIButton *)[window viewWithTag:1000];
    button.hidden = YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
